﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Aufgabe_6_Planets
{
    class EnglertMichaelMond : DrawableGameComponent
    {
        // Rotationsgeschwindigkeit und Rotationsstartpunkt
        float rotationSpeed = MathHelper.ToRadians(1.0f), rotation = 0;
        private Model moon;
        private ContentManager content;
        private Game game;
        private Matrix moonMatrix, viewMatrix, projectionMatrix, worldMatrix;
        Effect e;
        // Drehrichtung des Mondes
        private int direction = 1;

        public Matrix WorldMatrix
        { get; set; }

        public Matrix ViewMatrix
        { get; set; }

        public Matrix ProjectionMatrix
        { get; set; }



        public EnglertMichaelMond(Game game, Matrix viewMatrix, Matrix projectionMatrix, Matrix worldMatrix)
            : base(game)
        {
            this.viewMatrix = viewMatrix;
            this.projectionMatrix = projectionMatrix;
            this.worldMatrix = worldMatrix;
            this.game = game;
        }



        public override void Initialize()
        {
            this.content = this.game.Content;
            base.Initialize();
        }



        protected override void LoadContent()
        {
            this.moon = content.Load<Model>("models\\EnglertMichaelModelMoon");
            e = content.Load<Effect>("effects\\EnglertMichaelTiefenshader");

            foreach (ModelMesh mesh in moon.Meshes)
            {
                foreach (ModelMeshPart effect in mesh.MeshParts)
                {
                    effect.Effect = e;
                }
            }

            base.LoadContent();
        }



        public override void Update(GameTime gameTime)
        {
            // immer wieder um rotationSpeed (Gradzahl) weiter rotieren
            rotation = rotation + rotationSpeed * direction;

            // um 1 LE auf X-Achse verschieben, dann um 3° rotieren, dann um 10 LE auf X-Achse weiter schieben und dann um 1° rotieren
            moonMatrix = Matrix.CreateScale(0.15f) * Matrix.CreateTranslation(new Vector3(1.0f, 0.0f, 0.0f)) *
                         Matrix.CreateRotationY(rotation * 3) * Matrix.CreateTranslation(new Vector3(10.0f, 0.0f, 0.0f)) *
                         Matrix.CreateRotationY(rotation);

            base.Update(gameTime);
        }



        // Ändert die Drehrichtung des Planeten
        public void reverse()
        {
            this.direction *= (-1);
        }



        public override void Draw(GameTime gameTime)
        {

            // Mond zeichnen
            foreach (ModelMesh mesh in moon.Meshes)
            {
                foreach (ModelMeshPart effect in mesh.MeshParts)
                {
                    effect.Effect.Parameters["matViewProjection"].SetValue(moonMatrix * WorldMatrix * ViewMatrix * ProjectionMatrix);
                    effect.Effect.Parameters["fNearClipPlane"].SetValue(-105.000000f);
                    effect.Effect.Parameters["fFarClipPlane"].SetValue(5.000000f);
                }
                mesh.Draw();
            }

            base.Draw(gameTime);
        }
    }
}
